package cn.weekdragon.gscore.adapter.filter.impl;

import cn.hutool.core.util.StrUtil;
import cn.weekdragon.gscore.adapter.filter.BotFilter;
import cn.weekdragon.gscore.adapter.model.EventExt;
import cn.weekdragon.gscore.adapter.model.message.Message;
import cn.weekdragon.gscore.adapter.model.message.Plain;
import java.util.Objects;

public class StripSuffixFilter implements BotFilter {

    private final String suffix;

    public StripSuffixFilter(String suffix) {
        this.suffix = suffix;
    }

    @Override
    public boolean doFilter(EventExt eventExt) {
        eventExt.setTempText(null);
        Plain lastPlain = null;
        for (Message message : eventExt.getInMessage().toList()) {
            if(message instanceof Plain plain){
                String text = plain.getText();
                if(StrUtil.isEmpty(text)){
                    continue;
                }
                lastPlain = plain;
            }
        }
        if(Objects.nonNull(lastPlain)){
            String text = lastPlain.getText();
            if(text.endsWith(suffix)){
                lastPlain.setText(text.substring(suffix.length()));
            }
        }
        return false;
    }
}
